<?php

class Zend_View_Helper_Js extends Zend_View_Helper_Abstract {
	const JS_SRC = '<script type="text/javascript" src="%s"></script>';
	const JS_SCRIPT = '
<script type="text/javascript">
%s
</script>';
	const JS_READY_FN = '$(document).ready(function() {
%s
})';

	private $urls = array();
	private $scripts = array();
	private $readyScripts = array();

	public function js() {
		return $this;
	}

	public function urls() {
		$view = $this->view;
		return
		$this->view->dojo().
		$this->view->jQuery().
		$this->toJs('%s', $this->urls, function ($url) use($view) {
			return sprintf(Zend_View_Helper_Js::JS_SRC, $view->reformatBaseUrl($url));
		});
	}

	public function readyScripts() {
		return $this->toJs(sprintf(self::JS_SCRIPT, self::JS_READY_FN), $this->readyScripts);
	}

	public function scripts() {
		return $this->toJs(self::JS_SCRIPT, $this->scripts);
	}

	public function addUrl($url) {
		return $this->add($url, $this->urls);
	}

	public function addScript($script) {
		return $this->add($script, $this->scripts);
	}

	public function addReadyScript($readyScript) {
		$this->jQuery();
		return $this->add($readyScript, $this->readyScripts);
	}


	public function gite() {
		return $this->addUrl('js/gite.nouette.js');
	}

	public function dojo() {
		if (!$this->view->dojo()->isEnabled()) {
			$this->view->dojo()
			->setCdnVersion('1.5')
			->setDjConfigOption('usePlainJson',true)
			->setDjConfigOption('parseOnLoad', true)
			->addStyleSheetModule('dijit.themes.tundra')
			->requireModule("dojox.validate.regexp")
			->enable();
		}
		return $this;
	}

	public function jQuery() {
		if (!$this->view->jQuery()->isEnabled()) {
			$this->view->jQuery()
			->setVersion('1.9.1')
			->enable();
		}
		return $this;
	}

	public function jQueryUi() {
		$this->jQuery();
		if (!$this->view->jquery()->uiIsEnabled()) {
			$this->view->jquery()
			->setUiVersion('1.10.2')
			->uiEnable();
			$this->view->headLink()->prependStylesheet("http://ajax.googleapis.com/ajax/libs/jqueryui/".$this->view->jquery()->getUiVersion()."/themes/start/jquery-ui.css");
		}
		return $this;
	}

	public function jQueryEasing() {
		return $this->jQuery()->addUrl("library/jquery.easing.1.3.js");
	}

	public function jQueryCycle() {
		return $this->jQueryEasing()->jQueryMigrate()->addUrl("library/jquery.cycle.all.min.js");
	}

	public function JQueryAnythingSlider() {
		return $this->jQueryEasing()->addUrl("library/jquery.anythingslider.js");
	}

	public function jQuerySlider() {
		return $this->jQuery()->addUrl("library/slider.js");
	}

	public function jQueryEasySlider() {
		return $this->jQuery()->addUrl("library/easySlider1.7.js");
	}

	public function jQueryFeatureList() {
		return $this->jQuerySlider()->addUrl("library/jquery.featureList-1.0.0.js");
	}

	public function jQueryTipsy() {
		return $this->jQuery()->addUrl("library/jquery.tipsy.js");
	}

	public function jQueryTools() {
		return $this->jQuery()->addUrl("library/jquery.tools.min.js");
	}

    public function jQueryMigrate() {
        return $this->jQuery()->addUrl("http://code.jquery.com/jquery-migrate-1.1.0.js");
    }

    public function jQueryGalleriffic() {
        $this->view->headLink()->prependStylesheet($this->view->reformatBaseUrl("library/galleriffic/css/galleriffic-3.css"));
        return $this->jQuery()->addUrl("library/galleriffic/js/jquery.galleriffic.js");
    }

    public function jQueryHistory() {
        return $this->jQuery()->addUrl("library/galleriffic/js/jquery.history.js");
    }

    public function jQueryOpacityRollover() {
        return $this->jQuery()->addUrl("library/galleriffic/js/jquery.opacityrollover.js");
    }

	public function jQueryTransition() {
		return $this->jQuery()->addUrl("/library/jquery.transition-0.2.1.pack.js");
	}

    public function jQueryTransit() {
        return $this->jQuery()->addUrl("/library/jquery.transit.min.js");
    }

	public function jQueryTransitionGallery(array $params) {
		$readyScript = '$(\'#'.$params['id'].'\').transition({';
		unset($params['id']);
		foreach ($params as $key => $value) {
			$readyScript .= $key.': '.$value.',';
		}
		$readyScript = substr($readyScript, 0, -1);
		$readyScript .= '});';
		return $this->jQueryTransition()->addReadyScript($readyScript);
	}

	public function analyticsScript() {
		if ('production' == APPLICATION_ENV) {
			$this->addScript(
"var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20692397-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();");
		}
		return $this;
	}

	public function externalLinks() {
		return $this->jQuery()->addReadyScript('$("a.external").attr("target", "_blank");');
	}

	private function toJs($coating, array $scripts, $fn = NULL) {
		if (count($scripts) > 0) {
			$scripts_mapped = $scripts;
			if (is_callable($fn)) {
				$scripts_mapped = array_map($fn,  $scripts);
			}
			return sprintf($coating, implode('
', $scripts_mapped)).'
';
		}
		return '';
	}

	private function add($element, array &$elements) {
		if (!in_array($element, $elements)) {
			array_push($elements, $element);
		}
		return $this;
	}
}